<template>
  <transition name="move" tag="div">
    <div class="ratings">
      <scroll :data="ratings" class="scroll">
        <div class="main">
          <div class="overview">
            <div class="left border-right">
              <h1 class="score">{{sellerInfo.score}}</h1>
              <div class="title">综合评分</div>
              <div class="rank">高于周边商家{{sellerInfo.rankRate}}</div>
            </div>
            <div class="right">
              <div class="score-wrapper">
                <div class="title">服务态度</div>
                <star :size="36" :score="sellerInfo.serviceScore"></star>
                <span class="score">{{sellerInfo.serviceScore}}</span>
              </div>
              
              <div class="score-wrapper">
                <div class="title">商品评分</div>
                <star :size="36" :score="sellerInfo.foodScore"></star>
                <span class="score">{{sellerInfo.foodScore}}</span>
              </div>

              <div class="delivery-wrapper">
                <span class="title">送达时间</span>
                <span class="delivery-time">{{sellerInfo.deliveryTime}}分钟</span>
              </div>
            </div>
          </div>

          <split-space></split-space>

          <rating-select
            :ratings="ratings"
            :select-type="selectType"
            :only-content="onlyContent"
            @changeCurrentSelectType="handleSelectTypeChange"
            @changeOnlyContent="handleContentChnage"
          />

          <ul class="rating-wrapper">
            <li 
              class="rating-item border-bottom" 
              v-for="(rating, index) in ratings" 
              :key="index"
              v-show="needShow(rating.rateType, rating.text)"
            >
              <div class="avatar">
                <img class="pic" width="28" height="28" :src="rating.avatar" alt="">
              </div>

              <div class="content">
                <h1 class="name">{{rating.username}}</h1>
                
                <div class="star-wrapper">
                  <star :size="24" :score="rating.score"></star>
                  <span class="delivery" v-show="rating.deliveryTime">{{rating.deliveryTime}}分钟送达</span>
                </div>
                
                <p class="text">{{rating.text}}</p>
                
                <div class="recommend" v-show="rating.recommend.length">
                  <span class="icon icon-thumb_up"></span>
                  <span class="item border" v-for="(item, index) in rating.recommend" :key="index">
                    {{item}}
                  </span>
                </div>

                <div class="time">{{rating.rateTime | formatDate}}</div>
              </div>
            </li>
          </ul>
        </div>
      </scroll>
    </div>
  </transition>
</template>

<script>
import Scroll from '@/base/scroll/scroll'
import Star from '@/components/star/star'
import SplitSpace from '@/components/split-space/split-space'
import RatingSelect from '@/components/rating-select/rating-select'

import {ERR_OK} from '@/api/config'
import { formateDateTo } from '@/assets/js/date.js'

const ALL = 2

export default {
  name: 'Ratings',
  data() {
    return {
      ratings: [],
      selectType: ALL,
      onlyContent: true
    }
  },
  props: {
    sellerInfo: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  methods: {
    handleSelectTypeChange(currentSelectType) {
      this.selectType = currentSelectType
    },
    handleContentChnage(currentContentType) {
      this.onlyContent = currentContentType
    },
    needShow(type, text) {
      
      if(this.onlyContent && !text) return false
      
      if(this.selectType === ALL) {
        return true
      } else {
        return type === this.selectType
      }
    },
    _getRatings() {
      this.$axios
        .get('/api/ratings')
        .then(res => {
          res = res.data
          if(res.errno === ERR_OK) {
            this.ratings = res.data
          }
        })
        .catch(err => {
          throw new Error(err)
        })
    }
  },
  mounted() {
    setTimeout(() => {
      this._getRatings()
    }, 20)
  },
  filters: {
    formatDate(time) {
      let date = new Date(time)
      return formateDateTo(date, 'yyyy-MM-dd hh:mm')
    }
  },
  components: {
    Scroll,
    Star,
    SplitSpace,
    RatingSelect
  }
}
</script>

<style lang="stylus" scoped>
  @import '~@/assets/stylus/mixin'
  .ratings
    overflow hidden
    position absolute
    top 174px
    left 0
    bottom 0
    width 100%
    z-index 100
    background #ffffff
    &.move-enter-active
    &.move-leave-active
      transition all .5s
    &.move-enter
    &.move-leave-to
      transform translate3d(-100%, 0, 0)
    .scroll
      width 100%
      height 100%
    .overview
      display flex
      padding 18px
      .left
        flex 0 0 137px
        width 137px
        padding 6px 0
        border-color(rgba(7,17,27,.1))
        text-align center
        @media only screen and (max-width: 320px)
          flex 0 0 100px
          width 100px
        .score
          margin-bottom 6px
          line-height 28px
          font-size 24px
          color rgb(255,153,0)
        .title
          margin-bottom 8px
          line-height 12px
          font-size 12px
          color rgb(7,17,27)
        .rank
          line-height 10px
          font-size 10px
          color rgb(147, 153, 159)
      .right
        flex 1
        padding 6px 0 6px 24px
        @media only screen and (max-width: 320px)
          padding-left 6px
        .score-wrapper
          margin-bottom 8px
          font-size 0
          .title
            display inline-block
            vertical-align top
            line-height 18px
            font-size 12px
            color rgb(7,17,27)
          .star
            display inline-block
            vertical-align top
            margin 0 12px
          .score
            display inline-block
            line-height 18px
            vertical-align top
            font-size 12px
            color rgb(255,153,0)
        .delivery-wrapper
          font-size 0
          .title
            line-height 18px
            font-size 12px
            color rgb(7,17,27)
          .delivery-time
            padding-left 12px
            font-size 12px
            color rgb(147,153,159)
    .rating-wrapper
      padding 0 18px
      .rating-item
        display flex
        padding 18px 0
        border-color(rgba(7,17,27,.1))
        &:last-child
          border-none()
        .avatar
          flex 0 0 28px
          width 28px
          margin-right 12px
          .pic
            border-radius 50%
        .content
          flex 1
          position relative
          .name
            line-height 12px
            font-size 10px
            color rgb(7,17,27)
            margin-bottom 4px
          .star-wrapper
            margin-bottom 6px
            font-size 0
            .star
              display inline-block
              vertical-align top
              margin-right 6px
            .delivery
              display inline-block
              vertical-align top
              line-height 12px
              font-size 10px
              color rgb(147,153,159)
          .text
            margin-bottom 8px
            line-height 18px
            color rgb(7,17,27)
            font-size 12
          .recommend
            line-height 16px
            font-size 0
            .icon
            .item
              display inline-block
              margin 0 8px 4px 0
              vertical-align middle
              font-size 9px
            .icon
              color rgb(0,160,220)
            .item
              padding 0 6px
              border-color(rgba(7,17,27,.1))
              border-radius 1px
              color rgb(147, 153, 159)
          .time
            position absolute
            top 0
            right 0
            line-height 12px
            font-size 10px
            color rgb(147,153,159)
</style>

